//定义js命名空间
Ext.namespace('FSBS.FsbsQmTrsf');
//TODO : 完善数据结构
//DateTime -- date , decimal -- float
// { name: 'Department.Name', mapping: 'Department.Name' },
// { name: 'Department'},
//FsbsQmTrsf FieldArray
FSBS.FsbsQmTrsf.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'int' },
	{ name: 'TrsfNo', mapping: 'Id' },
	{ name: 'Trtype', type: 'string' },
	{ name: 'Opusid', type: 'string' },
	{ name: 'Otdpid', type: 'string' },
	{ name: 'Oudpus', type: 'string' },
	{ name: 'Indpid', type: 'string' },
	{ name: 'Indpus', type: 'string' },
	{ name: 'Exoutm', type: 'date' },
	{ name: 'Exartm', type: 'date' },
	{ name: 'Acoutm', type: 'date' },
	{ name: 'Acartm', type: 'date' },
	{ name: 'Reason', type: 'string' },
	{ name: 'Description', type: 'string' },
	{ name: 'ApprHisId', type: 'string' },
	{ name: 'Trstate', type: 'int' },
	{ name: 'SmsTime', type: 'date' },
	{ name: 'IsDelete', type: 'bool' },
	{ name: 'Version', type: 'int' },
	{ name: 'CreateUser', type: 'string' },
	{ name: 'CreateTime', type: 'date' }
);

//FsbsQmTrsf JsonReader
FSBS.FsbsQmTrsf.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FSBS.FsbsQmTrsf.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: FSBS.FsbsQmTrsf.BaseFieldsArray
		}, cfg));
	}
});

//FsbsQmTrsf JsonStore
FSBS.FsbsQmTrsf.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FSBS.FsbsQmTrsf.JsonStore.superclass.constructor.call(this, Ext.applyIf({
			QryUrl: '/FsbsQmTrsf/GetPlist', //查询Url
			UpdUrl: '/FsbsQmTrsf/SaveOrUpdate', //更新记录Url
			DelUrl: '/FsbsQmTrsf/Delete', //删除记录Url
			fields: FSBS.FsbsQmTrsf.BaseFieldsArray,
			sortInfo: {
				field: 'CreateTime',
				direction: 'DESC' // or 'DESC' (case sensitive for local sorting)
			}
		}, cfg));
	}
});

//FsbsQmTrsf GridPanel
FSBS.FsbsQmTrsf.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// User property
	//基本处理Url
	baseUrl: '/FsbsQmTrsf/',
	//提交保存Url
	formSubmitUrl: '/FsbsQmTrsf/SaveOrUpdate',
	//审批处理Url
	approveUrl: '/FsbsQmTrsf/Approve',
	// 调拨分类 trtype01 外派 trtype02 调拨
	trType: 'trtype01',
	//状态
	itemState: '',
	// Property
	store: new FSBS.FsbsQmTrsf.JsonStore(),
	itemGridEditor: new Infrastructure.BaseGridEditor({
		menuid: ''
	}),

	//constructor
	initComponent: function () {
		//prepare
		var itemGrid = this;
		this.itemEditRecord = new this.store.recordType({
			Trtype: this.trType,
			Otdpid: CurrentUserStore.data.items[0].data.DepartmentId
		});
		var trTypeName = this.trType == 'trtype01' ? '调拨' : '外派';
		//set store params
		this.store.setBaseParam('state', this.itemState);
		this.store.setBaseParam('trtype', this.trType);

		// The filter.
		var gridFilter = new Ext.ux.grid.GridFilters({
			encode: true,
			filters: [{
				dataIndex: 'Id',
				type: 'string'
			}, {
				dataIndex: 'Trtype',
				type: 'list',
				options: Infrastructure.Params.DictFilter('Trtype')
			}, {
				dataIndex: 'Opusid',
				type: 'string'
			}, {
				dataIndex: 'Otdpid',
				type: 'list',
				options: Infrastructure.Params.DepartmentFilter()
			}, {
				dataIndex: 'Oudpus',
				type: 'string'
			}, {
				dataIndex: 'Indpid',
				type: 'list',
				options: Infrastructure.Params.DepartmentFilter()
			}, {
				dataIndex: 'Indpus',
				type: 'string'
			}, {
				dataIndex: 'Exoutm',
				type: 'string'
			}, {
				dataIndex: 'Exartm',
				type: 'date'
			}, {
				dataIndex: 'Acoutm',
				type: 'date'
			}, {
				dataIndex: 'Acartm',
				type: 'date'
			}, {
				dataIndex: 'Reason',
				type: 'string'
			}, {
				dataIndex: 'Reason',
				type: 'list',
				options: Infrastructure.Params.DictFilter('tfrson')
			}, {
				dataIndex: 'Trstate',
				type: 'list',
				options: Infrastructure.Params.KeyValueFilter(FSBS.Params.CarTransferStateStore)
			}]
		});

		// selModel
		var selModel = new Ext.grid.CheckboxSelectionModel({
			header: '',
			singleSelect: true,
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					if (sm.getCount() != 1) {
						itemGrid.delBtn.setDisabled(true);
						itemGrid.btnToApprove.setDisabled(true);
						itemGrid.btnSendOut.setDisabled(true);
						itemGrid.btnRecive.setDisabled(true);
						itemGrid.btnStoreIn.setDisabled(true);
					}
					else {
						var s = itemGrid.getSelectionModel().getSelections()[0];
						//待提交                    
						itemGrid.delBtn.setDisabled(s.data.Trstate != 0);
						itemGrid.btnToApprove.setDisabled(s.data.Trstate != 0);
						itemGrid.btnSendOut.setDisabled(s.data.Trstate != 2);
						itemGrid.btnRecive.setDisabled(s.data.Trstate != 3);
						itemGrid.btnStoreIn.setDisabled(s.data.Trstate != 4);
					}
				}
			}
		});

		var approveColumn = new Infrastructure.InfraApproveLog.ApproveColumn({
			width: 25,
			editable: false,
			approveUrl: this.approveUrl
		});

		// 调拨显示的列
		var gridColumnTr = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				selModel,
				approveColumn,
				{
					header: '调拨单编号',
					dataIndex: 'Id'
				}, {
					header: "类型",
					dataIndex: 'Trtype',
					xtype: 'infra_dictcolumn',
					dictCode: 'tftype'
				}, {
					header: "经办人",
					dataIndex: 'Opusid',
					editable: true,
					editor: new Infrastructure.Params.InfraUserByDeptCombobox(),
					xtype: 'infra_usercolumn'
				}, {
					header: "调出",
					dataIndex: 'Otdpid',
					xtype: 'infra_deptcolumn'
				}, {
					header: "调出仓管",
					dataIndex: 'Oudpus',
					xtype: 'infra_usercolumn'
				}, {
					header: "调入",
					dataIndex: 'Indpid',
					editable: true,
					editor: new Infrastructure.Params.DepartmentCombobox(),
					xtype: 'infra_deptcolumn'
				}, {
					header: "调入仓管",
					dataIndex: 'Indpus',
					xtype: 'infra_usercolumn'
				}, {
					header: "预期出发",
					width: 150,
					dataIndex: 'Exoutm',
					xtype: 'datecolumn',
					editable: true,
					editor: new Ext.form.DateField({
						format: 'Y-m-d H:i'
					}),
					format: 'Y-m-d H:i'
				}, {
					header: "预期到达",
					width: 150,
					dataIndex: 'Exartm',
					xtype: 'datecolumn',
					editable: true,
					editor: new Ext.form.DateField({
						format: 'Y-m-d H:i'
					}),
					format: 'Y-m-d H:i'
				}, {
					header: "实际出发",
					dataIndex: 'Acoutm',
					xtype: 'datecolumn',
					format: 'Y-m-d H:i'
				}, {
					header: "实际到达",
					dataIndex: 'Acartm',
					xtype: 'datecolumn',
					format: 'Y-m-d H:i'
				}, {
					header: "事由",
					dataIndex: 'Reason',
					xtype: 'infra_dictcolumn',
					dictCode: 'tfrson',
					editable: true
				}, {
					header: "状态",
					dataIndex: 'Trstate',
					xtype: 'infra_KeyValueColumn',
					dataStore: FSBS.Params.CarTransferStateStore
				}
			],
			defaults: {
				sortable: true,
				editable: false,
				editor: {
					xtype: 'textfield'
				}
			}
		});

		// 外派显示的列
		var gridColumnSd = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				selModel,
				approveColumn,
				{
					header: '外派单编号',
					dataIndex: 'Id'
				}, {
					header: "类型",
					dataIndex: 'Trtype',
					xtype: 'infra_dictcolumn',
					dictCode: 'tftype',
					hidden: true
				}, {
					header: "经办人",
					dataIndex: 'Opusid',
					editable: true,
					editor: new Infrastructure.Params.InfraUserByDeptCombobox(),
					xtype: 'infra_usercolumn'
				}, {
					header: "仓库",
					dataIndex: 'Otdpid',
					xtype: 'infra_deptcolumn'
				}, {
					header: "出库",
					dataIndex: 'Oudpus',
					xtype: 'infra_usercolumn'
				}, {
					header: "入库",
					dataIndex: 'Indpus',
					xtype: 'infra_usercolumn'
				}, {
					header: "预期出发",
					dataIndex: 'Exoutm',
					xtype: 'datecolumn',
					editable: true,
					editor: new Ext.form.DateField(),
					format: 'm-d H:i'
				}, {
					header: "预期到达",
					dataIndex: 'Exartm',
					xtype: 'datecolumn',
					editable: true,
					editor: new Ext.form.DateField(),
					format: 'm-d H:i'
				}, {
					header: "实际出发",
					dataIndex: 'Acoutm',
					xtype: 'datecolumn',
					format: 'm-d H:i'
				}, {
					header: "实际到达",
					dataIndex: 'Acartm',
					xtype: 'datecolumn',
					format: 'm-d H:i'
				}, {
					header: "事由",
					dataIndex: 'Reason'
				}, {
					header: "状态",
					dataIndex: 'Trstate',
					xtype: 'infra_KeyValueColumn',
					dataStore: FSBS.Params.CarTransferStateStore
				}
			],
			defaults: {
				sortable: true,
				editable: false,
				editor: {
					xtype: 'textfield'
				}
			}
		});

		// PageBar
		var pagebBar = new Infrastructure.BasePagingToolbar({
			store: this.store,
			plugins: this.plugins
		});

		// 常用工具栏
		var toolBar = [{
			xtype: 'tbaraddbtn',
			menuid: '010401',
			handler: this.insertEntity,
			scope: itemGrid
		}, {
			xtype: 'tbardelbtn',
			menuid: '010402',
			handler: this.delEntity,
			scope: this
		}, {
			xtype: 'tbarmenubtn',
			menuid: '010401',
			disabled: true,
			ref: '../btnToApprove',
			text: '提交审批',
			icon: '/Content/icons/key_go.png',
			handler: function () {
				itemGrid.tbarSubmit(itemGrid.baseUrl + 'ToApproveProcess', '确定提交审批吗?', '已提交,请等待审批。');
			}
		}, {
			xtype: 'tbarmenubtn',
			ref: '../btnSendOut',
			menuid: '010401',
			disabled: true,
			text: '提车',
			icon: '/Content/icons/lorry_go.png',
			handler: function () {
				itemGrid.tbarSubmit(itemGrid.baseUrl + 'SendOut', '确定提车出库吗?', '调派单已提出。');
			}
		}, {
			xtype: 'tbarmenubtn',
			ref: '../btnRecive',
			menuid: '010401',
			disabled: true,
			text: '接收',
			icon: '/Content/icons/lorry_link.png',
			handler: function () {
				itemGrid.tbarSubmit(itemGrid.baseUrl + 'Recive', '该调派单车辆已全部抵达仓库?确定要接收?', '调派单已接收,可以进行复核入库.');
			}
		}, {
			xtype: 'tbarmenubtn',
			ref: '../btnStoreIn',
			menuid: '010401',
			disabled: true,
			text: '入库',
			icon: '/Content/icons/lorry_add.png',
			handler: function () {
				itemGrid.tbarSubmit(itemGrid.baseUrl + 'StoreIn', '复核无误,调派单可以入库?', '调派单及商品车信息已复核入库。');
			}
		}, '-', {
			xtype: 'textfield',
			width: 100,
			id: 'q_query',
			ref: '../q_query',
			listeners: {
				specialkey: function (field, e) {
					if (e.getKey() == e.ENTER) {
						itemGrid.qryList(field.getValue());
					}
				}
			}
		}, {
			xtype: 'tbarqrybtn',
			handler: function (e) {
				itemGrid.qryList(itemGrid.q_query.getValue());
			},
			scope: itemGrid
		}, '-'];

		Ext.applyIf(this, {
			plugins: [this.itemGridEditor, gridFilter],
			sm: selModel,
			cm: this.trType == 'trtype01' ? gridColumnTr : gridColumnSd,
			tbar: toolBar,
			bbar: pagebBar,
			listeners: {
				'rowdblclick': this.onLoadEntity
			}
		});

		FSBS.FsbsQmTrsf.GridPanel.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/
	// 读取信息显示
	onLoadEntity: function () {
		var itemGrid = this;
		var selRecord = this.getSelectionModel().getSelected();
		if (selRecord == null) {
			JsHelper.ShowError('请选择记录编辑..');
			return -1;
		}
		var selId = selRecord.data.Id;
		var itemEditForm = new FSBS.FsbsQmTrsf.EditForm({
			dataId: selId,
			dataRecord: selRecord,
			submitUrl: this.formSubmitUrl,
			submitMsg: this.formSubmitMsg,
			waitMsg: '数据加载中...',
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						itemGrid.store.reload();
						JsHelper.ShowInfo('信息更新成功!');
					}
				}
			}
		});
		var itemEditWin = new FSBS.FsbsQmTrsf.EditWin({
			title: selRecord.data['QMInout'] == 'qmio_0' ? 'FSBS_汽贸管理_商品车入库单' : 'FSBS_汽贸管理_商品车出库单',
			items: [itemEditForm]
		});
		itemEditWin.show();
		itemEditForm.form.loadRecord(selRecord);
	}
});

// 调拨窗口
FSBS.FsbsQmTrsf.TransferPanel = Ext.extend(Infrastructure.BasePanel, {
	initComponent: function () {
		Ext.applyIf(this, {
			items: [{
				layout: 'column',
				// 注意 现在使用的是两列 要是三列或更多
				// 请改变下面的items
				// 并在上面定义每个列的组件集合
				items: [{
					columnWidth: .3,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 120,
						allowBlank: true,
						readOnly: false
					},
					items: [{
						fieldLabel: "编号",
						xtype: 'textfield',
						readOnly: true,
						name: 'TrsfNo'
					}, {
						fieldLabel: "类型",
						xtype: 'infra_dictcombo',
						filterValue: 'trtype',
						valueField: 'Code',
						readOnly: true,
						hiddenName: 'Trtype'
					}, {
						fieldLabel: "经办",
						xtype: 'infra_usercombo',
						hiddenName: 'Opusid'
					}, {
						fieldLabel: "事由",
						xtype: 'infra_dictcombo',
						filterValue: 'tfrson',
						hiddenName: 'Reason'
					}]
				}, {
					columnWidth: .35,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 130,
						allowBlank: true,
						readOnly: false
					},
					items: [{
						fieldLabel: "预计出发",
						xtype: 'datefield',
						format: 'Y-m-d H:i',
						value: new Date().add(Date.HOUR, 1),
						hideTrigger: true,
						name: 'Exoutm'
					}, {
						fieldLabel: "预计到达",
						xtype: 'datefield',
						format: 'Y-m-d H:i',
						value: new Date().add(Date.HOUR, 3),
						hideTrigger: true,
						name: 'Exartm'
					}, {
						fieldLabel: "调出仓库",
						xtype: 'infra_deptcombo',
						hiddenName: 'Otdpid',
						readOnly: true
					}, {
						fieldLabel: "调出仓管",
						xtype: 'infra_usercombo',
						hiddenName: 'Oudpus',
						emptyText: '',
						readOnly: true
					}, {
						fieldLabel: "申请时间",
						readOnly: true,
						hideTrigger: true,
						xtype: 'datefield',
						format: 'Y-m-d H:i:s',
						name: 'CreateTime'
					}]
				}, {
					columnWidth: .35,
					layout: 'form',
					bodyStyle: 'padding:1px',
					defaults: {
						width: 130,
						allowBlank: true,
						readOnly: true
					},
					items: [{
						fieldLabel: "实际出发",
						xtype: 'datefield',
						format: 'Y-m-d H:i',
						hideTrigger: true,
						name: 'Acoutm'
					}, {
						fieldLabel: "实际到达",
						xtype: 'datefield',
						format: 'Y-m-d H:i',
						hideTrigger: true,
						name: 'Acartm'
					}, {
						fieldLabel: "调入仓库",
						xtype: 'infra_deptcombo',
						hiddenName: 'Indpid'
					}, {
						fieldLabel: "调入仓管",
						xtype: 'infra_usercombo',
						emptyText: '',
						hiddenName: 'Indpus'
					}, {
						fieldLabel: "状态",
						xtype: 'infra_KeyValueCombo',
						hiddenName: 'Trstate',
						store: FSBS.Params.CarTransferStateStore
					}]
				}]
			}],
			reader: new FSBS.FsbsQmTrsf.JsonReader()
		});

		FSBS.FsbsQmTrsf.TransferPanel.superclass.initComponent.call(this);
	}
});

//FsbsQmTrsf EditForm
FSBS.FsbsQmTrsf.EditForm = Ext.extend(Infrastructure.BaseForm, {
	dataId: null,
	dataRecord: null,
	trType: 'trsf01',
	submitUrl: '/FsbsQmTrsf/SaveOrUpdate',

	initComponent: function () {
		var itemEditForm = this;
		var trtype = this.dataRecord == null ? this.trType : this.dataRecord.data['TrType'];
		var isEdit = this.dataRecord == null ? true : !this.dataRecord.data['Trstate'];

		var carStore = new Infrastructure.BaseJsonStore({
			QryUrl: '/FsbsQmTrsf/GetCarPlist', //查询Url
			UpdUrl: '/FsbsQmTrsf/SaveOrUpdateCarInfo', //更新记录Url
			DelUrl: '/FsbsQmTrsf/DeleteCars?trsfid=' + this.dataId, //删除记录Url
			fields: FSBS.FsbsQmCrif.BaseFieldsArray
		});
		carStore.setBaseParam('trsfid', this.dataId);
		var carGrid = new FSBS.FsbsQmCrif.GridPanel({
			baseUrl: '/FsbsQmTrsf/',
			//提交保存Url
			formSubmitUrl: '/FsbsQmTrsf/SaveOrUpdateCarInfo?trsfid=' + this.dataId,
			store: carStore,
			trsfid: this.dataId,
			height: 300,
			tbar: [{
				xtype: 'tbaraddbtn', //添加车辆
				menuid: '010401',
				hidden: !isEdit,
				handler: function () {
					carGrid.chooseItem();
				}
			}, {
				xtype: 'tbardelbtn', //删除车辆
				menuid: '010401',
				hidden: !isEdit,
				handler: function () {
					carGrid.delEntity();
				}
			}],
			chooseItem: function () {
				var carStoreGrid = new FSBS.FsbsQmCrif.GridPanel({
					carState: '1',
					height: 500,
					tbar: [{
						xtype: 'tbaraddbtn',
						text: '选择',
						handler: function (e) {
							carStoreGrid.tbarSubmit('/FsbsQmTrsf/AddCars?trsfid=' + carGrid.trsfid, '选定车辆添加到调派单吗?', '添加成功', 'afterChoose');
						}
					}],
					listeners: {
						'afterChoose': function (ele, rt) {
							if (rt) {
								carGrid.store.reload();
							}
						}
					}
				});
				var carStoreGridWin = new Infrastructure.BaseWindow({
					title: '请选择车辆',
					iconCls: 'car',
					width: 600,
					items: [carStoreGrid]
				});
				carStoreGridWin.show();
				carStoreGrid.store.load();
			}
		});

		Ext.applyIf(this, {
			items: [
				new FSBS.FsbsQmTrsf.TransferPanel(),
				carGrid
			],
			buttons: [{
				xtype: 'btnsubmit',
				menuid: '010401',
				handler: function () {
					itemEditForm.saveItem();
				},
				hidden: !isEdit,
				scope: itemEditForm
			}, {
				xtype: 'btncancle',
				handler: function () {
					itemEditForm.ownerCt.destroy();
				}
			}],
			reader: new FSBS.FsbsQmTrsf.JsonReader()
		});

		FSBS.FsbsQmTrsf.EditForm.superclass.initComponent.call(this);
		carGrid.store.load();
		//定义保存后执行的事件
		this.addEvents('afterSave');
	}
});

//FsbsQmStrc DetailWin
FSBS.FsbsQmTrsf.EditWin = Ext.extend(Infrastructure.BaseWindow, {
	title: 'FSBS-汽贸-商品车调拨单',
	iconCls: 'application_form',
	width: 800,

	initComponent: function () {
		FSBS.FsbsQmTrsf.EditWin.superclass.initComponent.call(this);
	}
});
